<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${cIntegralItem.id == null ? '添加' : '编辑'}积分</title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/formSelects-v4.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <style type="text/css">
        .index-outside {
            min-height: auto !important;
        }

        .index-outside > section .section-main {
            min-width: auto !important;
        }
    </style>
</head>
<body>
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post">
                    <input type="hidden" name="id" value="${cUserIntegral.id}"/>
                    <input type="hidden" name="memberId" value="${memberId}"/>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">分类</label>
                            <div class="layui-input-inline">
                                <select name="integralTypeId" lay-filter="types" id="types">
                                    <option value="请选择"></option>
                                    <c:forEach var="type" items="${cIntegralTypes}">
                                        <option value="${type.id}" ${type.id == cUserIntegral.integralTypeId ? 'selected="selected"' : ''}>${type.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                            <div style="height: 50px"></div>
                            <label class="layui-form-label">积分项</label>
                            <div class="layui-input-inline" style="width: 340px">
                                <select name="integralItemId" lay-filter="items" id="items">
                                </select>
                            </div>
                            <div style="height: 50px"></div>
                            <label class="layui-form-label">分数 :</label>
                            <div class="layui-input-inline" style=" margin-top: 3px;">
                                <span class="active-red" id="itemFractionVal" style="font-size: 20px"></span>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>
</div>
<script>

    var form, laydate, formcache;
    $(function () {
        layui.use(['form', 'laydate'], function () {
            form = layui.form, laydate = layui.laydate;
            //自定义验证规则
            form.verify({
                name: function (value) {
                    if (value.trim() == "") {
                        return '分类名不能为空';
                    }
                },
                itemFraction: function (value) {
                    if (value.trim() == "") {
                        return "请填写分数";
                    }
                    if (value <= 0) {
                        return "请填写正确的分数";
                    }
                }
            });

            // 积分类型
            form.on('select(types)', function (data) {
                var typeId = data.value;
                var itemId = '';
                loadIntegralItem(typeId, itemId);
            });

            // 积分类型
            form.on('select(items)', function (data) {
                $("#itemFractionVal").html("")
                var itemFractionVal = $(data.elem).find("option:selected").attr("fractionVal");
                $("#itemFractionVal").html(itemFractionVal);
            });

            if ('${not empty cUserIntegral.id}' == 'true') {
                var typeId = '${cUserIntegral.integralTypeId}';
                var itemId = '${cUserIntegral.integralItemId}';
                loadIntegralItem(typeId, itemId);
            }
        });
    });
    function loadIntegralItem(typeId, itemId) {
        $.post("${ctx}/member/integral/integralItem/listAll.do", {
            "typeId": typeId,
            "memberId": '${memberId}'
        }, function (data) {
            var fraction = '';
            var items = new Array();
            items.push("<option value = ''>" + "选择积分项" + "</option>");
            for (var i = 0; i < data.length; i++) {
                if (itemId == data[i].id) {
                    items.push("<option value = '" + data[i].id + "' fractionVal =  '" + data[i].itemFraction + "'selected='selected'>" + data[i].name + "</option>");
                    fraction = data[i].itemFraction;
                } else {
                    items.push("<option value = '" + data[i].id + "' fractionVal =  '" + data[i].itemFraction + "'>" + data[i].name + "</option>");
                }
            }
            $("#items").html(items.join(""));
            $("#itemFractionVal").html(fraction);

            form.render('select');
        });
    }

    function submitForm(data) {
        var result = true;
        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: $('#myForm').serialize(),
            dataType: 'json',
            success: function (res) {
                if (res.success) {
                    util.layerMsgSuccess("提交成功")
                } else {
                    util.layerMsgError(res.description);
                    result = false;
                }
            },
            url: '${ctx}/member/integral/userIntegral/saveOrUpdate.do'
        });

        return result;

        txz.submitObject(data.elem, function (callBack) {
            var formData = getFormData();
            var action = $("#myForm").attr("action");
            txz.ajaxRequest({
                method: 'post',
                url: action,
                saveCache: true,
                cacheObj: formcache,
                params: formData,
                callBack: function (res) {
                    typeof callBack === 'function' && callBack();
                    if (res.success) {
                        util.layerMsgSuccess("提交成功", function () {
                            location.href = "${ctx}/integral/integralItem/list.do";
                        });
                    } else {
                        util.layerMsgError("提交失败")
                    }
                }
            });
        })
    }

    function getFormData() {
        var formData = util.serializeForm($('#myForm').serializeArray());
        return formData;
    }
</script>
</body>
</html>